//
//  YKProfileViewController.m
//  WeChat
//
//  Created by 吴高烽 on 16/5/31.
//  Copyright © 2016年 吴高烽. All rights reserved.
//

#import "YKProfileViewController.h"
#import "XMPPvCardTemp.h"
#import "YKEditvCardViewController.h"
@interface YKProfileViewController ()<YKEditvCardViewControllerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *avatarImgView;// 个人头像
@property (weak, nonatomic) IBOutlet UILabel *nickNameL;// 昵称
@property (weak, nonatomic) IBOutlet UILabel *weChatNumL;// 微信号
@property (weak, nonatomic) IBOutlet UILabel *orgNameL;// 公司
@property (weak, nonatomic) IBOutlet UITableViewCell *orgNameCell;
@property (weak, nonatomic) IBOutlet UILabel *departmentL;// 部门
@property (weak, nonatomic) IBOutlet UITableViewCell *departmentCell;
@property (weak, nonatomic) IBOutlet UILabel *jobL;// 职位
@property (weak, nonatomic) IBOutlet UITableViewCell *jobCell;
@property (weak, nonatomic) IBOutlet UILabel *emailL;// 邮箱
@property (weak, nonatomic) IBOutlet UITableViewCell *emailCell;
@property (weak, nonatomic) IBOutlet UITableViewCell *teleCell;
@property (weak, nonatomic) IBOutlet UILabel *teleL;//  电话
@property (weak, nonatomic) IBOutlet UITableViewCell *nickCell;
@property (weak, nonatomic) IBOutlet UITableViewCell *weChatNumCell;
@property (nonatomic ,strong) UIImagePickerController *imgPC ;

@end

@implementation YKProfileViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //    temp  有些电子名片的xml没有解析，需要补充
    XMPPvCardTemp *myvCard = [YKXMPPTool shareYKXMPPTool].vCard.myvCardTemp ;//名片对象
    if(myvCard.photo){
        self.avatarImgView.image =[UIImage imageWithData:myvCard.photo];
    }
    //    微信号 （显示用户名）
    self.weChatNumCell.detailTextLabel.text =[@"微信号:" stringByAppendingString:[YKAccount shareAccount].loginUser];

//        self.nickNameL.text = myvCard.nickname;
    self.nickCell.detailTextLabel.text = myvCard.nickname;
//    self.orgNameL.text = myvCard.orgName;
    self.orgNameCell.detailTextLabel.text = myvCard.orgName;
    if (myvCard.orgUnits.count>0) {
        self.departmentCell.detailTextLabel.text = myvCard.orgUnits[0];
//        self.departmentL.text =myvCard.orgUnits[0];
    }
//    self.jobL.text = myvCard.title ;
    self.jobCell.detailTextLabel.text = myvCard.title;
    
//    self.teleL.text = myvCard.note;
    self.teleCell.detailTextLabel.text = myvCard.note;
//    self.emailL.text = myvCard.mailer;
    self.emailCell.detailTextLabel.text= myvCard.mailer ;
    NSArray *emails = myvCard.emailAddresses ;
    self.emailCell.detailTextLabel.text = emails[0];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *selectCell  =[tableView cellForRowAtIndexPath:indexPath];
    switch (selectCell.tag) {
        case 0:
            YKLog(@"换头像");
            [self changeAvatar];
            break;
         case 1:
        {
//            YKLog(@"进入下一个控制器");
//            toEditVC  sender:传参
            [self performSegueWithIdentifier:@"toEditVC" sender:selectCell];
        
        }
            break;
            case 2:
            YKLog(@"不做操作");
            break;
        default:
            break;
    }

}
/**
 *  更换图片
 */
-(void)changeAvatar{
    UIAlertController *acton=[UIAlertController alertControllerWithTitle:@"请选择" message:@"" preferredStyle:UIAlertControllerStyleActionSheet ];
/*/    UIAlertActionStyleDefault ,
//    UIAlertActionStyleCancel,
//    UIAlertActionStyleDestructive Destructive//破坏性的，红色警告
 */
    UIAlertAction *defaultAction =[UIAlertAction actionWithTitle:@"照相" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        YKLog(@"照相");

    }];
    UIAlertAction *alertAction =[UIAlertAction actionWithTitle:@"图片库" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        YKLog(@"图片库");
        _imgPC =[[UIImagePickerController alloc]init];
        _imgPC.delegate = self ;
        _imgPC.allowsEditing = YES ;
        _imgPC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:_imgPC animated:YES completion:nil];
    }];
    UIAlertAction *cancelAction =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        YKLog(@"取消");
    }];
    [acton addAction:defaultAction];
    [acton addAction:alertAction];
    [acton addAction:cancelAction];
    [self presentViewController:acton animated:YES completion:nil ];
    
}


/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
//    获取目标控制器
    id destVc =segue.destinationViewController;
    // Pass the selected object to the new view controller.
//    设置编辑电子名片控制器的cell属性
    if ([destVc isKindOfClass:[YKEditvCardViewController class]]) {
        YKEditvCardViewController *editVC = destVc ;
        editVC.nickCell = sender;
        editVC.delegate = self ;
    }
    
}
-(void)editvCardViewController:(YKEditvCardViewController *)editVC didFinshedSave:(id)sender{
    YKLog(@"YKEditvCardViewController代理方法被调动");
//    把跟新的数据保存到服务中 1. 获取电子名片模块；2.重写vCard的属性
    XMPPvCardTemp *myvCard =[YKXMPPTool shareYKXMPPTool].vCard.myvCardTemp;
//    重新设置图片
    myvCard.photo = UIImageJPEGRepresentation(self.avatarImgView.image, 0.75);
    myvCard.nickname = self.nickCell.detailTextLabel.text;
    if (self.departmentCell.detailTextLabel.text != nil) {
        myvCard.orgName = self.orgNameCell.detailTextLabel.text;
    }
    myvCard.title = self.teleCell.detailTextLabel.text ;
    myvCard.note = self.teleCell.detailTextLabel.text ;
    myvCard.mailer = self.emailCell.detailTextLabel.text ;
    NSString *emailStr = self.emailCell.detailTextLabel.text ;
    myvCard.emailAddresses = @[emailStr];
//    把数据保存到服务器 ，内部实现数据上传（所有内容，包括图片）
    [[YKXMPPTool shareYKXMPPTool].vCard updateMyvCardTemp:myvCard];

}
#pragma mark - 图片选择器的代理
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
//    点击choose后调用这个方法
    YKLog(@"%@",info);
    UIImage *editedImg =info[UIImagePickerControllerEditedImage];
    self.avatarImgView.image = editedImg;
//    移除图片选择的控制器
    [self dismissViewControllerAnimated:YES completion:nil];
    [self editvCardViewController:nil didFinshedSave:nil];
}
@end
